<script setup lang="ts">
import { onMounted, ref } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import { FormProps } from "./utils/types";
// import { usePublicHooks } from "./hooks";
// import { baseUrl } from "@/utils/http";

import { getRoleList } from "@/api/role";
const roleList = ref<any[]>([]);
onMounted(async () => {
  const { data } = await getRoleList();
  data.forEach((value, index, array) => {
    if (value.roleKey === "admin") {
      value.userType = 0;
    } else if (value.roleKey === "teacher") {
      value.userType = 1;
    } else if (value.roleKey === "student") {
      value.userType = 2;
    }
  });

  roleList.value = data;
});

const changeRole = val => {
  console.log(val);
};

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    id: 0,
    nickName: "",
    avatar: "",
    phonenumber: "",
    sex: 0,
    status: 0,
    isFire: 0,
    userType: 1,
    roleId: 0
  })
});

const ruleFormRef = ref();
// const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="100px"
  >
    <el-row :gutter="30">
      <re-col>
        <el-form-item label="昵称" prop="nickName">
          <el-input
            v-model="newFormInline.nickName"
            clearable
            placeholder="请输入昵称"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            v-model="newFormInline.phonenumber"
            clearable
            placeholder="请输入手机号码"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="性别" prop="sex">
          <el-select v-model="newFormInline.sex" clearable placeholder="Select">
            <el-option label="男" value="0" />
            <el-option label="女" value="1" />
          </el-select>
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="角色类型" prop="userType">
          <el-select
            v-model="newFormInline.userType"
            clearable
            @change="changeRole"
            placeholder="Select"
          >
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.userType + ''"
            />
          </el-select>
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="状态">
          <el-switch
            v-model="newFormInline.status"
            inline-prompt
            :active-value="0"
            :inactive-value="1"
            active-text="启用"
            inactive-text="停用"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="上热门">
          <el-switch
            v-model="newFormInline.isFire"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            active-text="上热门"
            inactive-text="默认"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>

<style scoped>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  width: 178px;
  height: 178px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}
</style>
